package com.koron.housing.statistics.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.housing.contract.bean.param.HousingContractBillParam;
import com.koron.housing.contract.bean.vo.HousingContractStatistics;
import com.koron.housing.excel.BillBooleanStringConverter;
import com.koron.housing.excel.SameRowMargeStrategy;
import com.koron.housing.info.bean.query.HousingInfoStatisticsQuery;
import com.koron.housing.info.bean.vo.HousingInfoStatistics;
import com.koron.housing.staff.bean.query.HousingStaffStatisticsQuery;
import com.koron.housing.staff.bean.vo.HousingStaffStatistics;
import com.koron.housing.statistics.service.HousingStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/**
 * @author jzp
 * @since 2022/3/1
 */
@Api(tags = "过渡房统计接口")
@RestController
@RequestMapping("housingStatistics")
public class HousingStatisticsController {

    @Autowired
    private HousingStatisticsService statisticsService;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "租赁统计")
    @GetMapping("lease")
    public Response<PageInfo<HousingStaffStatistics>> lease(PageQuery pageQuery, HousingStaffStatisticsQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), statisticsService, "pageLease", PageInfo.class, pageQuery, query));
    }

    @SneakyThrows
    @ApiOperation(value = "租赁统计导出")
    @GetMapping("leaseExport")
    public void leaseExport(HttpServletResponse response, HousingStaffStatisticsQuery query) {
        List<HousingStaffStatistics> list = ADOConnection.runTask(getCurrDs(), statisticsService, "listLease", List.class, query);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("租赁统计", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), HousingStaffStatistics.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("租赁统计")
                .doWrite(list);
    }

    @ApiOperation(value = "房源统计")
    @GetMapping("info")
    public Response<List<HousingInfoStatistics>> info(HousingInfoStatisticsQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), statisticsService, "listInfo", List.class, query));
    }

    @SneakyThrows
    @ApiOperation(value = "房源统计导出")
    @GetMapping("infoExport")
    public void infoExport(HttpServletResponse response, HousingInfoStatisticsQuery query) {
        List<HousingInfoStatistics> list = ADOConnection.runTask(getCurrDs(), statisticsService, "listInfo", List.class, query);

        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("房源统计", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), HousingInfoStatistics.class)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .sheet("房源统计")
                .doWrite(list);
    }

    @ApiOperation(value = "发票统计")
    @GetMapping("bill")
    public Response<List<HousingContractStatistics>> bill(HousingContractBillParam query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), statisticsService, "listBill", List.class, query));
    }

    @SneakyThrows
    @ApiOperation(value = "发票统计导出")
    @GetMapping("billExport")
    public void billExport(HttpServletResponse response, HousingContractBillParam query) {
        List<HousingContractStatistics> list = ADOConnection.runTask(getCurrDs(), statisticsService, "listBill", List.class, query);
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode(query.getYear()+"年度发票统计", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-Disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        BillBooleanStringConverter billBooleanStringConverter = new BillBooleanStringConverter();
        EasyExcel.write(response.getOutputStream(), HousingContractStatistics.class)
                .registerConverter(billBooleanStringConverter)
                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .registerWriteHandler(new SameRowMargeStrategy<HousingContractStatistics>(list, new HashSet<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7)), HousingContractStatistics::getCode))
                .sheet(query.getYear()+"年度发票统计")
                .doWrite(list);
    }

}
